﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WFONE
{
    class CDataBase
    {
        public static string sDataFilePath = "";
        public static string sSlDataFileName = "";
        public static string sDlDataFileName = "";
        public static StreamWriter swSlDataWriter = null;
        public static FileStream fsDataFileStream;
        public static StreamReader swSlDataReader = null;
        public static StreamWriter swDlDataWriter = null;
        public static StreamReader swDlDataReader = null;
        //数据与日志相类似
        public static void SetDatabaseFile()
        {
            //获取当前程序所在路径
            sDataFilePath = Environment.CurrentDirectory + "\\Data";

            //文件名默认
            sSlDataFileName = "Sldatabase.txt";
            sDlDataFileName = "Dldatabase.txt";
        }
        public static void WriteData()
        {
            //设置文件路径
            SetDatabaseFile();

            //创建数据文件夹和数据文件
            if (!Directory.Exists(sDataFilePath))
            {
                Directory.CreateDirectory(sDataFilePath);
            }
            string sDataFullFileName = sDataFilePath + "\\" + sSlDataFileName;
            // 判断文件是否存在，不存在则创建，否则读取值显示到窗体
            if (!File.Exists(sDataFullFileName))
            {
                fsDataFileStream = new FileStream(sDataFullFileName, FileMode.Create, FileAccess.Write);//创建写入文件 
            }

            else
            {
                fsDataFileStream = new FileStream(sDataFullFileName, FileMode.Open, FileAccess.Write);
            }

            //打开单面数据文件
            swSlDataWriter = new StreamWriter(fsDataFileStream);

            //写入文件
            //获取设备链表的链表头
            CDevice dDlist = new CDevice();
            dDlist = CDeviceList.cDeviceListHead;
            //获取第一个设备的数据链表的链表头
            CDeDataSl dlist = new CDeDataSl() ;
            dlist = dDlist.cDeDataSingleListHead;



            while (dDlist != null)
            {
                swSlDataWriter.Write("DeviceNumber:" + dDlist.name + ";");
                while (dlist != null)
                {
                    swSlDataWriter.Write("dataTime:" + dlist.datetime + ";");
                    swSlDataWriter.Write("Rs:" + dlist.dRs + ";");
                    swSlDataWriter.Write("Rx:" + dlist.dRx + ";");
                    swSlDataWriter.Write("Gp:" + dlist.dGp + ";");
                    swSlDataWriter.Write("Rp:" + dlist.dRp + ";");
                    swSlDataWriter.Write("PAngle:" + dlist.dPAngle + ";");
                    swSlDataWriter.Write("\n");
                    swSlDataWriter.Flush();
                    dlist = dlist.next;
                }
                dDlist = dDlist.next;
            }
            swSlDataWriter.Close();
        }
        public static void ReadData()
        {
            SetDatabaseFile();
            string sDataFullFileName = sDataFilePath + "\\" + sSlDataFileName;

            fsDataFileStream = new FileStream(sDataFullFileName, FileMode.Open, FileAccess.Read);//读文件
            swSlDataReader = new StreamReader(fsDataFileStream);

            List<string> DeviceName = new List<string>();
         
            
            //获取设备链表的设备的指针
            CDevice cDevicePointer = new CDevice();

            
            while (swSlDataReader.Peek() != -1)
            {

                string sRead = swSlDataReader.ReadLine();//读取一行字符流
                string[] sread = AnalysisData(sRead);//获取该行里的设备名字 和一组设备数据  
                CDeDataSl cDeDataNode = new CDeDataSl() ;  //暂存读取到的设备数据(不含设备的名字)

                cDeDataNode.datetime = DateTime.Parse(sread[1]);
                cDeDataNode.dRs = double.Parse(sread[2]);
                cDeDataNode.dRx = double.Parse(sread[3]);
                cDeDataNode.dGp = double.Parse(sread[4]);
                cDeDataNode.dRp = double.Parse(sread[5]);
                cDeDataNode.dPAngle = double.Parse(sread[6]);
            //    cDeDataNode.dA1 = double.Parse(sread[7]);
             //   cDeDataNode.dA2 = double.Parse(sread[8]);

        
                    if (DeviceName.Contains(sread[0]) )//设备链表中出存在这种设备
                    {
                        cDevicePointer = CDeviceList.SearchDevice(sread[0]);//获取设备的指针
                    }
                    else
                    {
                        DeviceName.Add(sread[0]);//更新临时设备名字链表
                        cDevicePointer = CDeviceList.AddDevice(sread[0]);//在设备链表中增加新设备
                         CDeviceList.DeviceNum++;

                    }
                    cDevicePointer.AddDeData(cDeDataNode);                     
                       
                }
            
            //开始读取设备的双面数据
            sDataFullFileName = sDataFilePath + "\\" + sDlDataFileName;
            fsDataFileStream = new FileStream(sDataFullFileName, FileMode.Open, FileAccess.Read);//读文件
            swDlDataReader = new StreamReader(fsDataFileStream);
            while (swDlDataReader.Peek() != -1)
            {

                string sRead = swDlDataReader.ReadLine();//读取一行字符流
                string[] sread = AnalysisData(sRead);//获取该行里的设备名字 和一组设备数据  
                CDeDataDl cDeDataNode = new CDeDataDl();  //暂存读取到的设备数据(不含设备的名字)

                cDeDataNode.datetime = DateTime.Parse(sread[1]);
                cDeDataNode.dR1s = double.Parse(sread[2]);
                cDeDataNode.dR1x = double.Parse(sread[3]);
                cDeDataNode.dG1p = double.Parse(sread[4]);
                cDeDataNode.dR1p = double.Parse(sread[5]);
                cDeDataNode.dPAngle1 = double.Parse(sread[6]);
                cDeDataNode.dR2s = double.Parse(sread[7]);
                cDeDataNode.dR2x = double.Parse(sread[8]);
                cDeDataNode.dG2p = double.Parse(sread[9]);
                cDeDataNode.dR2p = double.Parse(sread[10]);
                cDeDataNode.dPAngle2 = double.Parse(sread[11]);



                if (DeviceName.Contains(sread[0]))//设备链表中出存在这种设备
                {
                    cDevicePointer = CDeviceList.SearchDevice(sread[0]);//获取设备的指针
                }
                else
                {
                    DeviceName.Add(sread[0]);//更新临时设备名字链表
                    cDevicePointer = CDeviceList.AddDevice(sread[0]);//在设备链表中增加新设备
                    CDeviceList.DeviceNum++;

                }
                cDevicePointer.AddDeData(cDeDataNode);

            }
        }
        public static string[] AnalysisData(string sData)
        {
            //字符串处理                     
            string[] sstr = new string[2];         
            string[] stemp = sData.Split(';');
            string[] sdata = new string[stemp.Length];

            for (int index = 0; index < stemp.Length; index++)
            {
                sstr = stemp[index].Split('：');

                if (sstr[1] != null)
                {
                    sdata[index] = sstr[1];
                }
            }
            return sdata;
        }

    }
}
